import user from "../../../../class/Utils/User"
import http from "../../../../class/Utils/Http"
import tips from "../../../../class/Utils/Tips"
Page({
  data: {
    param:{'uid':wx.getStorageSync('user').userid,id:0},
    cate_list:[],
    cate_code:[],
    cate_form:'请选择',
    cate_index:-1,
    
    cate2_list:[],
    cate2_code:[],
    cate2_form:'请选择',
    cate2_index:-1,

    guojia_list:[],
    guojia_code:[],
    guojia_form:'请选择',
    guojia_index:-1,

    sheng_list:[],
    sheng_code:[],
    sheng_form:'请选择',
    sheng_index:-1,

    shi_list:[],
    shi_code:[],
    shi_form:'请选择',
    shi_index:-1,

    money_list:['货币单位','元','美元','欧元','日元'],
    money_index:0,

    unit_list:['/吨','/千克','/米','/平方米','/立方米'],
    unit_index:0,

    info:{},
    category:{},
    regional:{},
    regional_code:0,   // 最终地区代码
    country_code: 0,   //     国家代码
    category_code:0,   //     分类代码

    pic1:'',
    pic2:'',
    pic3:'',
    data:{}, // z最终要提交的数据集合
    disabled:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    tips.loading("加载中")
    var that =this
    var param = that.data.param
    //options.id = 3670492
    if(options.id){
      param.id = options.id
      that.setData({param:param})
    }
    that.info()
  },
  info:function(){
    var that = this
    http.req("/Api/product_detail",that.data.param,function (res) {
      console.log(res)
      if(res.pic1){
        that.setData({pic1:res.pic1})
        if(!/http/.test(res.pic1)){
          res.pic1 = "http://img.i.album.toocle.com/0-0-0-1/"+res.pic1
        }
      }
      if(res.pic2){
        that.setData({pic2:res.pic2})
        if(!/http/.test(res.pic2)){
          res.pic2 = "http://img.i.album.toocle.com/0-0-0-1/"+res.pic2
        }
      }
      if(res.pic3){
        that.setData({pic3:res.pic3})
        if(!/http/.test(res.pic3)){
          res.pic3 = "http://img.i.album.toocle.com/0-0-0-1/"+res.pic3
        }
      }
      if(res.my_unit.length){
        var my_unit = []
        for(var i=0; i<res.my_unit.length;i++){
          my_unit.push('/'+res.my_unit[i].name)
        }
        that.setData({unit_list:['数量单位'].concat(my_unit,that.data.unit_list)})
      }else{
        that.setData({unit_list:['数量单位'].concat(that.data.unit_list)})
      }
      res.state = res.state?res.state:-1
      res.id = res.id?res.id:0
      that.setData({
        info: res,
        state:res.state,
        id:res.id,
        category:res.category_list,
        regional:res.regional_list
      })
      
      var cate_list = []
      var cate_code = []
      for(var i=0; i<that.data.category.items.length;i++){
        cate_list.push(that.data.category.items[i].name)
        cate_code.push(that.data.category.items[i].code)
      }

      that.setData({
         cate_list:cate_list,
         cate_code:cate_code
      })
      if(that.data.info.category){
        var code = that.data.info.category.substring(0,2)
        for(var i=0; i<cate_code.length; i++){
          if(code == cate_code[i]){
            var cate_index = i
            break
          }
        }
        that.setData({
          cate_index:cate_index,
          cate_form:cate_list[cate_index],
          category_code:cate_code[cate_index]
        })

        /*分类  二级下拉  开始*/
        var cate2_item = 'items'+code
        var cate2_list = []
        var cate2_code = []
        for(var i=0; i<that.data.category[cate2_item].length;i++){
          cate2_list.push(that.data.category[cate2_item][i].name)
          cate2_code.push(that.data.category[cate2_item][i].code)
        }
        // console.log(cate2_list)
        // console.log(cate2_code)
        that.setData({
           cate2_list:cate2_list,
           cate2_code:cate2_code
        })
        /*二级选中*/
        if(that.data.info.category.length >=4){
          code = that.data.info.category.substring(0,4)
          for(var i=0; i<cate2_code.length; i++){
            if(code == cate2_code[i]){
              var cate2_index = i
              break
            }
          }
          that.setData({
            cate2_index:cate2_index,
            cate2_form:cate2_list[cate2_index],
            category_code:cate2_code[cate2_index]
          })
          /*二级分类结束*/
        }
      }
      // 国家
      var guojia_list = []
      var guojia_code = []
      for(var i=0; i<that.data.regional.items.length;i++){
        guojia_list.push(that.data.regional.items[i].name)
        guojia_code.push(that.data.regional.items[i].code)
      }
      // console.log(guojia_list)
      // console.log(guojia_code)
      that.setData({
         guojia_list:guojia_list,
         guojia_code:guojia_code
      })
      /*一级选中*/
      if(that.data.info.origin){
        code = that.data.info.origin.substring(0,2)
        for(var i=0; i<guojia_code.length; i++){
          if(code == guojia_code[i]){
            var guojia_index = i
            break
          }
        }
        that.setData({
          guojia_index:guojia_index,
          guojia_form:guojia_list[guojia_index],
          country_code:guojia_code[guojia_index],
          regional_code:guojia_code[guojia_index]
        })


        //省  二级下拉
        var sheng_item = 'items'+code
        var sheng_list = []
        var sheng_code = []
        for(var i=0; i<that.data.regional[sheng_item].length;i++){
          sheng_list.push(that.data.regional[sheng_item][i].name)
          sheng_code.push(that.data.regional[sheng_item][i].code)
        }
        // console.log(sheng_list)
        // console.log(sheng_code)
        that.setData({
           sheng_list:sheng_list,
           sheng_code:sheng_code
        })
        /*二级选中*/
        if(that.data.info.origin.length >=4){
          code = that.data.info.origin.substring(0,4)
          for(var i=0; i<sheng_code.length; i++){
            if(code == sheng_code[i]){
              var sheng_index = i
              break
            }
          }
          that.setData({
            sheng_index:sheng_index,
            sheng_form:sheng_list[sheng_index],
            regional_code:sheng_code[sheng_index]
          })

          // 市 三级下拉
          var shi_item = 'items'+code
          var shi_list = []
          var shi_code = []
          for(var i=0; i<that.data.regional[shi_item].length;i++){
            shi_list.push(that.data.regional[shi_item][i].name)
            shi_code.push(that.data.regional[shi_item][i].code)
          }
          // console.log(shi_list)
          // console.log(shi_code)
          that.setData({
             shi_list:shi_list,
             shi_code:shi_code
          })
          /*三级选中*/
          if(that.data.info.origin.length >=6){
            code = that.data.info.origin.substring(0,6)
            for(var i=0; i<shi_code.length; i++){
              if(code == shi_code[i]){
                var shi_index = i
                break
              }
            }
            that.setData({
              shi_index:shi_index,
              shi_form:shi_list[shi_index],
              regional_code:shi_code[shi_index]
            })
          }
          /*三级选中结束*/
        }
        /*二级选中结束*/
      }
      /*一级选中结束*/


      /*单位开始*/
      if(that.data.info.money){
        for(var i=0; i<that.data.money_list.length; i++){
          if(that.data.info.money == that.data.money_list[i]){
            that.setData({
              money_index:i
            })
            break
          }
        }
      }
      if(that.data.info.unit){
        for(var i=0; i<that.data.unit_list.length; i++){
          if(that.data.info.unit == that.data.unit_list[i]){
            that.setData({
              unit_index:i
            })
            break
          }
        }
      }
      /*单位结束*/
      tips.loaded()
    })
  },
  selectcate: function (e) {
    //console.log(e.detail.value)
    var that = this
    if(e.detail.value != that.data.cate_index){
      var code = that.data.cate_code[e.detail.value]
      var cate2_item = 'items'+code
      var cate2_list = []
      var cate2_code = []
      for(var i=0; i<that.data.category[cate2_item].length;i++){
        cate2_list.push(that.data.category[cate2_item][i].name)
        cate2_code.push(that.data.category[cate2_item][i].code)
      }
      // console.log(cate2_list)
      // console.log(cate2_code)
      that.setData({
        cate_index: e.detail.value,
        cate_form: that.data.cate_list[e.detail.value],

        cate2_list:cate2_list,
        cate2_code:cate2_code,
        cate2_form:'请选择',
        cate2_index:-1,
        category_code:code
      })
      console.log(that.data.category_code)
    }
  },
  selectcate2: function (e) {
    //console.log(e.detail.value)
    var that = this
    if(e.detail.value != that.data.cate2_index){
      that.setData({
        cate2_index: e.detail.value,
        cate2_form: that.data.cate2_list[e.detail.value],
        category_code:that.data.cate2_code[e.detail.value]
      })
      console.log(that.data.category_code)
    }
  },
  selectguojia: function (e) {
    //console.log(e.detail.value)
    var that = this
    if(e.detail.value != that.data.guojia_index){
      
      var code = that.data.guojia_code[e.detail.value]
      var sheng_item = 'items'+code
      var sheng_list = []
      var sheng_code = []
      for(var i=0; i<that.data.regional[sheng_item].length;i++){
        sheng_list.push(that.data.regional[sheng_item][i].name)
        sheng_code.push(that.data.regional[sheng_item][i].code)
      }
      //console.log(sheng_list)
      //console.log(sheng_code)
      that.setData({
        guojia_index: e.detail.value,
        guojia_form: that.data.guojia_list[e.detail.value],

        sheng_list:sheng_list,
        sheng_code:sheng_code,
        sheng_form:'请选择',
        sheng_index:-1,

        shi_list:[],
        shi_code:[],
        shi_form:'请选择',
        shi_index:-1,

        //regional_code:code,
        country_code:code
      })
      console.log(that.data.regional_code)
    }
  },
  selectsheng: function (e) {
    //console.log(e.detail.value)
    var that = this
    if(e.detail.value != that.data.sheng_index){
      var code = that.data.sheng_code[e.detail.value]
      var shi_item = 'items'+code
      var shi_list = []
      var shi_code = []
      for(var i=0; i<that.data.regional[shi_item].length;i++){
        shi_list.push(that.data.regional[shi_item][i].name)
        shi_code.push(that.data.regional[shi_item][i].code)
      }
      // console.log(shi_list)
      // console.log(shi_code)
      that.setData({
        sheng_index: e.detail.value,
        sheng_form: that.data.sheng_list[e.detail.value],

        shi_list:shi_list,
        shi_code:shi_code,
        shi_form:'请选择',
        shi_index:-1,
        regional_code:code
      })
      console.log(that.data.regional_code)
    }
  },
  selectshi: function (e) {
    //console.log(e.detail.value)
    var that = this
    if(e.detail.value != that.data.shi_index){
      that.setData({
        shi_index: e.detail.value,
        shi_form: that.data.shi_list[e.detail.value],
        regional_code:that.data.shi_code[e.detail.value]
      })
      console.log(that.data.regional_code)
    }
  },
  selectmoney: function (e) {
    var that = this
    that.setData({
        money_index: e.detail.value
    })
  },
  selectunit: function (e) {
    var that = this
    that.setData({
        unit_index: e.detail.value
    })
  },
  upimg: function (e) {
    console.log(e.currentTarget.dataset.type)
    var that = this
    wx.chooseImage({
      count :1,
      sizeType: ['original', 'compressed'],
      success: function (res) {
        //console.log(res)
        //console.log(wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], "base64"))
        if(res.tempFiles[0].size>5*1000*1000){
          tips.error("图片大于5M")
        }else{
          wx.getImageInfo({
            src: res.tempFilePaths[0],
            success (res) {
              var key = 'temp_pic'+e.currentTarget.dataset.type
              var val = 'pic'+e.currentTarget.dataset.type
              that.setData({
                [key]: res.path,
                [val]: 'data:image/'+res.type+';base64,'+wx.getFileSystemManager().readFileSync(res.path, "base64")
              })
            }
          })
        }
        
      }
    })
  },
  formSubmit:function(e){
    //console.log(e)
    var that = this
    var name = e.detail.value.name
    if(!name){
        tips.error("请输入产品名称")
        return false
    }
    if(that.data.category_code == 0){
      tips.error("请选择行业分类")
      return false
    }
    if(that.data.pic1 == ''){
      tips.error("请选择商品图片")
      return false
    }
    if(that.data.param.id){
      var method = 'change'
      var title = '更新商品'
    }else{
      var method = 'create'
      var title = '发布商品'
    }
    wx.showModal({
        title: title,
        content: '您确定要提交吗',
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确认')
            var data = {
                uid:that.data.param.uid,
                name:name,
                category:that.data.category_code,
                rank:e.detail.value.rank,
                model:e.detail.value.model,
                producer:e.detail.value.producer,
                price:e.detail.value.price,
                brand:e.detail.value.brand,
                origin:that.data.regional_code,
                intro:e.detail.value.intro,
            }
            if(that.data.money_index){
              data.money = that.data.money_list[that.data.money_index]
            }
            if(that.data.unit_index){
              data.unit = that.data.unit_list[that.data.unit_index]
            }
            if(that.data.param.id){
              data.id = that.data.param.id
            }
            if(that.data.pic1){
              data.pic1 = that.data.pic1
            }
            if(that.data.pic2){
              data.pic2 = that.data.pic2
            }
            if(that.data.pic3){
              data.pic3 = that.data.pic3
            }
            console.log(data)
            //return
            that.setData({disabled:1})
            tips.loading("提交中")
            http.req("/Api/product_"+method,data,function (res) {
              console.log(res)
              if(/successful/.test(res.exp)){
                tips.ok(title+"成功")
                // 成功后跳转
                that.setData({state:1})
                if(res.id){
                  var param = that.data.param
                  param.id = res.id
                  that.setData({param:param})
                }
                if(res.files.pic1){
                  that.setData({pic1:res.files.pic1})
                }
                if(res.files.pic2){
                  that.setData({pic2:res.files.pic2})
                }
                if(res.files.pic3){
                  that.setData({pic3:res.files.pic3})
                }
              }else if(res.exp == 'total_is_large'){
                tips.error('数量已最大')
              }else{
                tips.error(title+"失败")
              }
              that.setData({disabled:0})
              //tips.loaded()
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
    })
  },
  pdel:function(e){
    var that = this
    wx.showModal({
        title: '温馨提示',
        content: '您确定要删除吗',
        success: function(res) {
          if (res.confirm) {
            tips.loading("提交中")
            http.req("/Api/product_delete",that.data.param,function (res) {
              if(/successful/.test(res.exp)){
                tips.ok("删除成功")
                // 成功后跳转
                wx.redirectTo({
                  url: '/pages/main/myshangpin/list/list'
                })
              }else{
                tips.error("删除失败")
              }
              tips.loaded()
            })
          }
        }
    })
    
  },
  pable:function(e){
    var that = this
    tips.loading("提交中")
    http.req("/Api/product_able",that.data.param,function (res) {
      console.log(res)
      that.setData({state:res})
      if(res==1){
        tips.ok("上架成功")
      }else if(res==2){
        tips.ok("下架成功")
      }
      tips.loaded()
    })
  },


})